package com.leyou.item.controller;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.TbSpecGroupService;
import com.leyou.item.service.TbSpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecGroupController {

    @Autowired
    private TbSpecGroupService specGroupService;



    /*根据商品分类id 查询规格组数据*/
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> findSpecGroupByCategoryId(@RequestParam("id") Long id){

        List<SpecGroupDTO>    specGroupDTOList=specGroupService.findSpecGroupByCategoryId(id);

        return  ResponseEntity.status(HttpStatus.OK).body(specGroupDTOList);
    }



    @PutMapping("/group")
    public ResponseEntity<Void> update(@RequestBody SpecGroupDTO specGroupDTO){

            /*更新*/
            specGroupService.updateSpecGroup(specGroupDTO);

            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }
    @PostMapping("/group")
    public ResponseEntity<Void> save(@RequestBody SpecGroupDTO specGroupDTO) {
        /*新增*/
        specGroupService.saveSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /*删除*/
    @DeleteMapping("group/{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id){

        specGroupService.deleteSpecGroup(id);

        return ResponseEntity.status(HttpStatus.OK).build();

    }

   @GetMapping("/of/category")
    public ResponseEntity<List<SpecGroupDTO>>findSpecGroupDTO(@RequestParam("id") Long cid){
      // 根据categoryId查询规格参数组和组内参数
      List<SpecGroupDTO>  specGroupDTOList=specGroupService.findSpecGroupDTO(cid);

      return ResponseEntity.status(HttpStatus.OK).body(specGroupDTOList);
   }
 }
